package bibliOnline.util;

import bibliOnline.db.*;
import bibliOnline.task.*;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author luca
 */
public class BibliOnlineInitializer implements ServletContextListener {

    public static final String PROPERTIES_FILE_NAME = "system.properties";
    public static final long DEFAULT_INTERVAL = 5;
    private Properties systemProperties;
    private DBConnection connection;
    private ScheduledExecutorService expiredBookingScheduler;
    private ScheduledExecutorService expiringBookingScheduler;
    private ScheduledExecutorService expiringLoanScheduler;
    private ScheduledExecutorService begunBookingScheduler;
    private ScheduledExecutorService purchaseSuggestionScheduler;

    @Override
    public void contextInitialized(ServletContextEvent event) {
	System.out.println("INITIALIZING CONTEXT...");

	systemProperties = new Properties();
	//  contesto dell'applicazione web
	ServletContext context;
	context = event.getServletContext();

	try {
	    systemProperties.load(
		    context.getResourceAsStream("/WEB-INF/" + PROPERTIES_FILE_NAME));
	} catch (Exception ex) {
	    Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
	    return;
	}
	System.out.println("SYSTEM PROPERTIES READ");

	connection = new DBConnection(systemProperties);
	connection.init(event);

	long EXPIRED_BOOKING_INTERVAL,
		EXPIRING_BOOKING_INTERVAL,
		EXPIRING_LOAN_INTERVAL,
		BEGUN_BOOKING_INTERVAL,
		PURCHASE_SUGGESTION_INTERVAL;

	EXPIRED_BOOKING_INTERVAL = DEFAULT_INTERVAL;
	EXPIRING_BOOKING_INTERVAL = DEFAULT_INTERVAL;
	EXPIRING_LOAN_INTERVAL = DEFAULT_INTERVAL;
	BEGUN_BOOKING_INTERVAL = DEFAULT_INTERVAL;
	PURCHASE_SUGGESTION_INTERVAL = DEFAULT_INTERVAL;

	try {
	    EXPIRING_BOOKING_INTERVAL =
		    ConstantManager.get("BOOKING_EXPIRY_FOREWARNING").getValue();
	    EXPIRING_BOOKING_INTERVAL = EXPIRING_BOOKING_INTERVAL / 3;
	} catch (Exception ex) {
	    Logger.getLogger(BibliOnlineInitializer.class.getName()).log(Level.SEVERE, null, ex);
	}
	try {
	    EXPIRING_LOAN_INTERVAL =
		    ConstantManager.get("LOAN_EXPIRY_FOREWARNING").getValue();
	    EXPIRING_LOAN_INTERVAL = EXPIRING_LOAN_INTERVAL / 3;
	} catch (Exception ex) {
	    Logger.getLogger(BibliOnlineInitializer.class.getName()).log(Level.SEVERE, null, ex);
	}

	expiredBookingScheduler = Executors.newSingleThreadScheduledExecutor();
	expiredBookingScheduler.scheduleAtFixedRate(new ExpiredBookingTask(),
		0, EXPIRED_BOOKING_INTERVAL, TimeUnit.SECONDS);

	expiringBookingScheduler = Executors.newSingleThreadScheduledExecutor();
	expiringBookingScheduler.scheduleAtFixedRate(new ExpiringBookingTask(systemProperties),
		0, EXPIRING_BOOKING_INTERVAL, TimeUnit.SECONDS);

	expiringLoanScheduler = Executors.newSingleThreadScheduledExecutor();
	expiringLoanScheduler.scheduleAtFixedRate(new ExpiringLoanTask(systemProperties),
		0, EXPIRING_LOAN_INTERVAL, TimeUnit.SECONDS);

	begunBookingScheduler = Executors.newSingleThreadScheduledExecutor();
	begunBookingScheduler.scheduleAtFixedRate(new BegunBookingTask(systemProperties),
		0, BEGUN_BOOKING_INTERVAL, TimeUnit.SECONDS);

	purchaseSuggestionScheduler = Executors.newSingleThreadScheduledExecutor();
	purchaseSuggestionScheduler.scheduleAtFixedRate(new PurchaseSuggestionTask(systemProperties),
		0, PURCHASE_SUGGESTION_INTERVAL, TimeUnit.SECONDS);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
	System.out.println("DESTROYING CONTEXT...");

	expiredBookingScheduler.shutdown();
	expiringBookingScheduler.shutdown();
	expiringLoanScheduler.shutdown();
	begunBookingScheduler.shutdown();
	purchaseSuggestionScheduler.shutdown();

	connection.destroy(event);
    }
}
